package plus.xj17520.org.controller;

import plus.xj17520.basic.domain.BaseQuery;
import plus.xj17520.basic.util.PageList;
import plus.xj17520.org.domain.Department;
import plus.xj17520.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

//Controller + ResponseBody
@RestController
@RequestMapping("dept")
/**
 * @Api 作用在类上 value 里面填写的是当前类的文档描述
 */
@Api(value = "部门管理的接口文档",description = "部门管理的接口文档")
public class DeptController {
    @Autowired
    IDepartmentService departmentService;


    /**
     * 一般情况下,但凡传递一个对象,就需要使用psotmapping
     * 只有一个参数或者没有参数就是用get
     * @RequestBody:将请求Json参数转换成java对象
     *  @ResponseBody: 将java对象转换成json返回给前端,返回前端,禁止页面跳转,不走视图解析器
     *  接收前端请求四个要素:
     *  第一个: url路径:/dept/findPage
     *  第二个: 请求方式:post
     *  第三个:请求参数: page 当前页,keyword 查询条件
     *  第四个:响应参数: {total:'',list:"" }
     */
    //    @GetMapping
//    @PutMapping
//    @DeleteMapping
//    @PatchMapping
    @ApiOperation(value = "高级分页查询", notes = "高级分页查询")
    @PostMapping("/findPage")
    public PageList  findPage(@RequestBody BaseQuery departmentQuery){
       return departmentService.queryData(departmentQuery);
    }
    @ApiOperation(value = "树形结构查询", notes = "树形结构查询")
    @GetMapping("/findTree")
    public List<Department> findTree(){

        return departmentService.findTree();
    }
    @ApiOperation(value = "单个查询", notes = "单个查询")
    @GetMapping("findOne")
    public Department findOne( Long id){

        return departmentService.loadById(id);
    }

    //restfull风格发送请求,参数是直接拼接到路径后面
    //从路径上取参数需要两步:
    //1.路径上的参数需要加{}
    //2.蚕食上需要加 @PathVariable
    @ApiOperation(value = "单个删除", notes = "单个删除")
    @GetMapping("delete/{id}")
    public void delete( @PathVariable Long id){
         departmentService.remove(id);
    }



    /**
     * 1.url: "dept/add"
     * 2.请求方式: post
     * 3.请求参数:{"name":"研发部3","sn":"0003","managerId":350,"parentIds":[3,19]}
     * 4.响应参数:null
     */
    @ApiOperation(value = "单个新增", notes = "单个新增")
    @PostMapping("add")
    public void add( @RequestBody @Valid Department department){
        departmentService.save(department);
    }
    /**
     * 1.url:/dept/batchDelete
     * 2.post
     * 3.请求参数: [1,2,3]
     * 4.响应参数:null
     */
    @ApiOperation(value = "批量删除", notes = "批量删除")
    @PostMapping("batchDelete")
    public void batchDelete( @RequestBody List<Long> ids){
        departmentService.batchDelete(ids);
    }
}